#include "StdAfx.h"

void Shader::setValue(const std::string& param, void* pMemory, ui32 size) {
	auto handle = getParamHandle(param);
	if(handle == NULL)
		throw std::exception("Unable to find parameter!");

	mEffect->SetValue(handle, pMemory, size);
}

void Shader::setValue(D3DXHANDLE handle, void* value, ui32 size) {
	mEffect->SetValue(handle, value, size);
}

D3DXHANDLE Shader::getParamHandle(const std::string& param) {
	if(mParameters.find(param) != mParameters.end())
		return mParameters[param];

	D3DXHANDLE ret = mEffect->GetParameterByName(NULL, param.c_str());
	if(ret == NULL)
		return ret;

	mParameters[param] = ret;
	return ret;
}

void Shader::doBasicRender(std::function<void ()> fun) {
	UINT passes = 0;
	mEffect->Begin(&passes, 0);
	for(ui32 i = 0; i < passes; ++i) {
		mEffect->BeginPass(i);
		fun();
		mEffect->EndPass();
	}
	mEffect->End();
}